﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using YTCMS.Core;
using YTCMS.Domain.Entities;
using YTCMS.Services.Services;

namespace Admin.Controllers
{
    /// <summary>
    /// 栏目管理
    /// </summary>
    public class CmscolumnController : BaseController
    {
        private readonly IcmscolumnService _cmscolumn;
        private readonly ImoduleService _module;
        private readonly IproductsService _products;
        private readonly IcolumnproductService _columnproduct;
        public CmscolumnController(IcmscolumnService cmscolumn, ImoduleService module, IproductsService products, IcolumnproductService columnproduct)
        {
            this._cmscolumn = cmscolumn;
            this._module = module;
            this._products = products;
            this._columnproduct = columnproduct;
        }
        //
        // GET: /CmsColumn/
        public ActionResult Index(int? pageIndex = 1, int? pageSize = 10)
        {
            var data = _module.GetMany(s => s.State == (int)CommonState.正常, Order.desc, "sort");
            ViewData["module"] = data;
            int pcount = 0;
            var model = _cmscolumn.GetMany(s => true, Order.desc, "Sort");
            model = _cmscolumn.PageList(model, pageIndex.Value - 1, pageSize.Value, out pcount);
            ViewBag.PageSize = pageSize.Value;
            ViewBag.TotalCount = pcount;
            return View(model);
        }


        public ActionResult Search(int? mid, int? pageIndex = 1, int? pageSize = 10)
        {
            int pcount;
            var model = _cmscolumn.GetMany(s => true, Order.desc, "Sort");
            model = QueryHelper.WhereIf<cmscolumn>(model, s => s.ModuleId == mid, mid != null);
            model = _cmscolumn.PageList(model, pageIndex.Value - 1, pageSize.Value, out pcount);
            ViewBag.PageSize = pageSize.Value;
            ViewBag.TotalCount = pcount;
            return PartialView("List", model);
        }


        public ActionResult add()
        {
            return View();
        }
        [HttpPost]
        public ActionResult add(cmscolumn model)
        {
            if (ModelState.IsValid)
            {
                model.CreateTime = System.DateTime.Now;
                model.State = (int)CommonState.正常;
                _cmscolumn.Add(model);
            }
            return new JsonResult() { Data = new { d = true } };
        }


        



        public ActionResult Edit(int id)
        {
            var model = _cmscolumn.GetAsync(s => s.Id == id);
            return View(model);
        }
        [HttpPost]
        public ActionResult Edit(cmscolumn model)
        {
            if (ModelState.IsValid)
            {
                _cmscolumn.Update(model);
            }
            return new JsonResult() { Data = new { d = true } };
        }
      
        public ActionResult Delete(int id)
        {
            var model = _cmscolumn.Get(s => s.Id == id);
            _cmscolumn.Delete(model);
            return new JsonResult() { Data = true, JsonRequestBehavior = JsonRequestBehavior.AllowGet };
        }

        public ActionResult SetProduct(int cid, int? pageIndex = 1, int? pageSize = 10)
        {

            ViewBag.cid = cid;
            int pcount1 = 0, pcount2 = 0;

            var cp = _columnproduct.GetMany(s => s.State == (int)CommonState.正常 && s.Cid == cid, Order.desc, "Sort");

            var model = _products.GetMany(s => s.State == (int)CommonState.正常, Order.desc, "CreateTime");
            model = _products.PageList(model, pageIndex.Value - 1, pageSize.Value, out pcount1);
            ViewBag.PageSize = pageSize.Value;
            ViewBag.TotalCount = pcount1;
           

            cp = _columnproduct.PageList(cp, pageIndex.Value - 1, pageSize.Value, out pcount2);
            ViewBag.PageSize2 = pageSize.Value;
            ViewBag.TotalCount2 = pcount2;
            ViewData["cp"] = cp;
            return View(model);
        }

        public ActionResult ProductSearch(string pname, int? pageIndex = 1, int? pageSize = 10)
        {
            int pcount = 0;
            var model = _products.GetMany(s => true);
            model = QueryHelper.WhereIf<products>(model, s => s.Name.Contains(pname), !string.IsNullOrWhiteSpace(pname));
            model = _products.PageList(model, pageIndex.Value - 1, pageSize.Value, out pcount);
            ViewBag.PageSize = pageSize.Value;
            ViewBag.TotalCount = pcount;
            return PartialView("SetProductList", model);
        }

        public ActionResult columnproductSearch(int cid, string pname, int? pageIndex = 1, int? pageSize = 10)
        {
            int pcount = 0;
            var model = _columnproduct.GetMany(s => s.State == (int)CommonState.正常 && s.Cid == cid, Order.desc, "Sort");
            model = QueryHelper.WhereIf<columnproduct>(model, s => s.products.Name.Contains(pname), !string.IsNullOrWhiteSpace(pname));
            model = _columnproduct.PageList(model, pageIndex.Value - 1, pageSize.Value, out pcount);
            ViewBag.PageSize = pageSize.Value;
            ViewBag.TotalCount = pcount;
            return PartialView("columnproduct", model);
        }


        public ActionResult SetColumnProduct(string pid,int cid)
        {
            try
            {
                var cmscolumn = _cmscolumn.Get(s => s.Id == cid);
                var ps = pid.Split(',');
                foreach (var item in ps)
                {
                    _columnproduct.Add(new columnproduct() { Cid = cid, CreateTime = System.DateTime.Now, Pid = item.StrConvertInt(0), State = (int)CommonState.正常, Mid = cmscolumn.ModuleId, Sort =1 });
                }
            }
            catch (Exception)
            {
               return new JsonResult() { Data = new { d = false,msg="设置失败" } };
            }
            return new JsonResult() { Data = new { d = true } };
        }

        public ActionResult DeleteColumnProduct(int id)
        {
            var model = _columnproduct.Get(s => s.Id == id);
            _columnproduct.Delete(model);
            return new JsonResult() { Data = true, JsonRequestBehavior = JsonRequestBehavior.AllowGet };
        }

        public ActionResult SetOrder(int id,int sort)
        {
            try
            {
                var model = _columnproduct.Get(s => s.Id == id);
                model.Sort = sort;
                _columnproduct.Update(model);
            }
            catch (Exception)
            {
                
               return new JsonResult() { Data = new { d = false,msg="保存失败" } };
            }

            return new JsonResult() { Data = new { d = true } };
        }
	}
}